//请实现一个函数按照之字形顺序打印二叉树，即第一行按照从左到右的顺序打印，第二层按照从右到左的顺序打印，第三行再按照从左到右的顺序打印，其他行以此类推。 
//
// 
//
// 例如: 
//给定二叉树: [3,9,20,null,null,15,7], 
//
//     3
//   / \
//  9  20
//    /  \
//   15   7
// 
//
// 返回其层次遍历结果： 
//
// [
//  [3],
//  [20,9],
//  [15,7]
//]
// 
//
// 
//
// 提示： 
//
// 
// 节点总数 <= 1000 
// 
// Related Topics 树 广度优先搜索 二叉树 👍 127 👎 0


package leetcode.editor.cn1;

import com.leetcode.entity.TreeNode;

import java.util.*;

//Java：从上到下打印二叉树 III
public class CongShangDaoXiaDaYinErChaShuIiiLcof {
    public static void main(String[] args) {
        Solution solution = new CongShangDaoXiaDaYinErChaShuIiiLcof().new Solution();
        // TO TEST

        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.right.right = new TreeNode(5);

        List<List<Integer>> list = solution.levelOrder(root);
        StringBuilder sb = new StringBuilder();
        for (List<Integer> items : list) {
            sb.append("[");
            for (Integer item : items) {
                sb.append(item + ",");
            }
            sb.append("],");
        }
        System.out.println(sb.toString());
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public List<List<Integer>> levelOrder(TreeNode root) {
            if (root == null)
                return new ArrayList<>();
            Queue<TreeNode> queue = new ArrayDeque<>();
            List<List<Integer>> list = new ArrayList<>();
            queue.offer(root);
            // 表示先从左边开始
            int left = 0;
            while (!queue.isEmpty()) {
                List<Integer> list1 = new ArrayList<>();
                for (int i = queue.size(); i >= 1; i--) {
                    TreeNode node = queue.poll();
                    if (node != null) {
                        // 第一个肯定是根节点
                        list1.add(node.val);
                    }
                    // 依然从左到右
                    if (node.left != null) {
                        queue.offer(node.left);
                    }
                    if (node.right != null) {
                        queue.offer(node.right);
                    }
                }
                if (!list1.isEmpty()) {
                    if (left == 0) {
                        list.add(list1);
                        left = 1;
                    } else {
                        // 将list1进行倒序
                        List<Integer> list2 = new ArrayList<>();
                        int llen = list1.size();
                        for (int i = llen - 1; i >= 0; i--) {
                            list2.add(list1.get(i));
                        }
                        list.add(list2);
                        left = 0;
                    }
                }
            }
            return list;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
